package main

import (
    "fmt"
    "os"
    "time"
)

// LogWriter 日志写入器接口
type LogWriter interface {
    WriteLog(level string, message string) error
    Close() error
    GetWriterType() string
}

// ConsoleLogWriter 控制台日志写入器
type ConsoleLogWriter struct {
    colorEnabled bool
}

func (c *ConsoleLogWriter) WriteLog(level string, message string) error {
    timestamp := time.Now().Format("2006-01-02 15:04:05")
    if c.colorEnabled {
        color := c.getColorCode(level)
        fmt.Printf("\033[%sm[%s] %s - %s\033[0m\n", color, level, timestamp, message)
    } else {
        fmt.Printf("[%s] %s - %s\n", level, timestamp, message)
    }
    return nil
}

func (c *ConsoleLogWriter) getColorCode(level string) string {
    switch level {
    case "ERROR":
        return "31" // 红色
    case "WARN":
        return "33" // 黄色
    case "INFO":
        return "32" // 绿色
    default:
        return "37" // 白色
    }
}

func (c *ConsoleLogWriter) Close() error {
    fmt.Println("控制台日志写入器已关闭")
    return nil
}

func (c *ConsoleLogWriter) GetWriterType() string {
    return "控制台"
}

// FileLogWriter 文件日志写入器
type FileLogWriter struct {
    filename string
    file     *os.File
}

func (f *FileLogWriter) WriteLog(level string, message string) error {
    if f.file == nil {
        return fmt.Errorf("文件未打开")
    }
    timestamp := time.Now().Format("2006-01-02 15:04:05")
    logEntry := fmt.Sprintf("[%s] %s - %s\n", level, timestamp, message)
    _, err := f.file.WriteString(logEntry)
    return err
}

func (f *FileLogWriter) Close() error {
    if f.file != nil {
        fmt.Printf("文件日志写入器已关闭: %s\n", f.filename)
        return f.file.Close()
    }
    return nil
}

func (f *FileLogWriter) GetWriterType() string {
    return "文件"
}

// LogWriterFactory 日志写入器工厂
type LogWriterFactory struct{}

// CreateLogWriter 创建日志写入器
func (f *LogWriterFactory) CreateLogWriter(writerType string, config map[string]interface{}) (LogWriter, error) {
    switch writerType {
    case "console":
        colorEnabled := false
        if color, ok := config["color_enabled"].(bool); ok {
            colorEnabled = color
        }
        return &ConsoleLogWriter{colorEnabled: colorEnabled}, nil
        
    case "file":
        filename, ok := config["filename"].(string)
        if !ok {
            return nil, fmt.Errorf("文件日志写入器需要filename参数")
        }
        
        file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
        if err != nil {
            return nil, fmt.Errorf("无法打开文件 %s: %v", filename, err)
        }
        
        return &FileLogWriter{filename: filename, file: file}, nil
        
    default:
        return nil, fmt.Errorf("不支持的日志写入器类型: %s", writerType)
    }
}

// 使用演示
func main() {
    fmt.Println("=== 日志写入器工厂演示 ===")
    
    factory := &LogWriterFactory{}
    
    // 创建控制台写入器
    consoleConfig := map[string]interface{}{
        "color_enabled": true,
    }
    consoleWriter, _ := factory.CreateLogWriter("console", consoleConfig)
    consoleWriter.WriteLog("INFO", "这是一条信息日志")
    consoleWriter.WriteLog("ERROR", "这是一条错误日志")
    consoleWriter.Close()
    
    // 创建文件写入器
    fileConfig := map[string]interface{}{
        "filename": "app.log",
    }
    fileWriter, _ := factory.CreateLogWriter("file", fileConfig)
    fileWriter.WriteLog("INFO", "这是文件日志")
    fileWriter.Close()
}